home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / ISSUE10 / FILES / STRM2U.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-02-27  |  3.3 KB  |  161 lines

  1. unit Strm2u;
  2.  
  3. interface
  4.  
  5. uses
  6.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  7.   Forms, Dialogs, StdCtrls, ExtCtrls, Buttons;
  8.  
  9. type
  10.   TPointData = class(TComponent)
  11.   public
  12.     FX, FY: Word;
  13.     constructor CreateXY(AOwner: TComponent; AX, AY: Word);
  14.     procedure SwapXY;
  15.   published
  16.     property X: Word read FX write FX default 0;
  17.     property Y: Word read FY write FY default 0;
  18.   end;
  19.  
  20.   TForm1 = class(TForm)
  21.     PaintBox1: TPaintBox;
  22.     Bevel1: TBevel;
  23.     MakeBtn: TButton;
  24.     SaveBtn: TButton;
  25.     LoadBtn: TButton;
  26.     SwapBtn: TButton;
  27.     procedure FormCreate(Sender: TObject);
  28.     procedure FormDestroy(Sender: TObject);
  29.     procedure PaintBox1Paint(Sender: TObject);
  30.     procedure MakeBtnClick(Sender: TObject);
  31.     procedure SaveBtnClick(Sender: TObject);
  32.     procedure LoadBtnClick(Sender: TObject);
  33.     procedure SwapBtnClick(Sender: TObject);
  34.   private
  35.     PointList: TList;
  36.     procedure ClearPoints;
  37.   end;
  38.  
  39. var
  40.   Form1: TForm1;
  41.   Pt: TPointData;
  42.   Loop: Integer;
  43.  
  44. const
  45.   DataFile = 'POINTS2.DAT';
  46.  
  47. implementation
  48.  
  49. {$R *.DFM}
  50.  
  51. constructor TPointData.CreateXY(AOwner: TComponent; AX, AY: Word);
  52. begin
  53.   inherited Create(AOwner);
  54.   FX := AX;
  55.   FY := AY;
  56. end;
  57.  
  58. procedure TPointData.SwapXY;
  59. begin
  60.   Tag := FX;
  61.   FX := FY;
  62.   FY := Tag;
  63. end;
  64.  
  65. procedure TForm1.ClearPoints;
  66. begin
  67.   while PointList.Count > 0 do
  68.   begin
  69.     TPointData(PointList[0]).Free;
  70.     PointList.Delete(0);
  71.   end;
  72. end;
  73.  
  74. procedure TForm1.FormCreate(Sender: TObject);
  75. begin
  76.   PointList := TList.Create;
  77. end;
  78.  
  79. procedure TForm1.FormDestroy(Sender: TObject);
  80. begin
  81.   PointList.Free;
  82. end;
  83.  
  84. procedure TForm1.PaintBox1Paint(Sender: TObject);
  85. begin
  86.   for Loop := 0 to PointList.Count - 1 do
  87.   begin
  88.     Pt := TPointData(PointList[Loop]);
  89.     if Loop = 0 then
  90.       PaintBox1.Canvas.MoveTo(Pt.X, Pt.Y)
  91.     else
  92.       PaintBox1.Canvas.LineTo(Pt.X, Pt.Y)
  93.   end;
  94. end;
  95.  
  96. procedure TForm1.MakeBtnClick(Sender: TObject);
  97. begin
  98.   ClearPoints;
  99.   for Loop := 1 to Random(40) + 1 do
  100.   begin
  101.     Pt := TPointData.CreateXY(Self,
  102.             Random(PaintBox1.Width),
  103.             Random(PaintBox1.Height));
  104.     PointList.Add(Pt);
  105.     PaintBox1.Invalidate;
  106.   end;
  107. end;
  108.  
  109. procedure TForm1.SaveBtnClick(Sender: TObject);
  110. var
  111.   Stream: TFileStream;
  112. begin
  113.   Stream := TFileStream.Create(DataFile, fmCreate);
  114.   try
  115.     for Loop := 0 to PointList.Count - 1 do
  116.     begin
  117.       Pt := TPointData(PointList.Items[Loop]);
  118.       Stream.WriteComponent(Pt);
  119.     end;
  120.   finally
  121.     Stream.Free;
  122.   end;
  123.   ClearPoints;
  124.   Invalidate;
  125. end;
  126.  
  127. procedure TForm1.LoadBtnClick(Sender: TObject);
  128. var
  129.   Stream: TFileStream;
  130. begin
  131.   ClearPoints;
  132.   Stream := TFileStream.Create(DataFile, fmOpenRead or fmShareDenyWrite);
  133.   try
  134.     while Stream.Position <> Stream.Size do
  135. {$ifdef OldHat}
  136.     begin
  137.       Pt := TPointData.Create(Self);
  138.       Stream.ReadComponent(Pt);
  139.       PointList.Add(Pt);
  140.     end;
  141. {$else}
  142.       PointList.Add(Stream.ReadComponent(nil));
  143. {$endif}
  144.   finally
  145.     Stream.Free;
  146.   end;
  147.   Invalidate;
  148. end;
  149.  
  150. procedure TForm1.SwapBtnClick(Sender: TObject);
  151. begin
  152.   for Loop := 0 to PointList.Count - 1 do
  153.     TPointData(PointList[Loop]).SwapXY;
  154.   Invalidate;
  155. end;
  156.  
  157. initialization
  158.   Randomize;
  159.   RegisterClass(TPointData);
  160. end.
  161.